package com.wanban.downloader

import android.content.Intent
import android.util.Log
import com.blankj.utilcode.util.ServiceUtils
import com.blankj.utilcode.util.Utils
import com.wanban.downloader.db.DownloadEntity
import com.wanban.downloader.listener.OnDownloadListener
import com.wanban.downloader.manager.BaseDownloadManager
import com.wanban.downloader.service.DownloadService
import com.wanban.downloader.manager.DownloadManager
import com.wanban.downloader.proxy.LocalServer
import com.wanban.downloader.utils.FileDownloadUtils

object MultiDownloadManager {

    const val TAG = BaseDownloadManager.TAG
    // 每个视频的下载线程数量
    const val THREAD_COUNT = 5
    // 下载管理器
    private val downloadManager: BaseDownloadManager
    private val proxyLocalServer: LocalServer
    init {
        downloadManager = DownloadManager()
        proxyLocalServer = LocalServer()
    }

    fun getDownloadManager(): BaseDownloadManager {
        return downloadManager
    }

    fun init(listener: OnDownloadListener) {
        downloadManager.setDownloadListener(listener)
        val intent = Intent(Utils.getApp(), DownloadService::class.java)
        intent.putExtra("action", -1)
        ServiceUtils.startService(intent)

        try {
            // 开启本地代理
            if (!proxyLocalServer.isAlive) {
                proxyLocalServer.start()
            }
        } catch (e: Exception) {
            Log.e(TAG, "代理服务器启动失败 =>" + e.message)
        }
    }


    /**
     * 通过本地文件的地址，获取本地http的地址
     * @param localUrl
     * @return
     **/
    fun getLocalProxyUrl(fileName: String): String? {
        val proxyM3U8 = FileDownloadUtils.getOrCreateProxyM3U8File(fileName)?:return null
        return String.format("http://127.0.0.1:%d%s", LocalServer.PORT, proxyM3U8)
    }

    fun hasDownloadingTask(success: (hasDownloading: Boolean) -> Unit) {
        downloadManager.hasDownloadingTask(success)
    }

    fun resumeAllDownload() {
        val intent = Intent(Utils.getApp(), DownloadService::class.java)
        intent.putExtra("action", 0)
        ServiceUtils.startService(intent)
    }

    fun startDownload(task: DownloadEntity) {
        val intent = Intent(Utils.getApp(), DownloadService::class.java)
        intent.putExtra("task", task)
        intent.putExtra("action", 1)
        ServiceUtils.startService(intent)
    }

    fun stopDownload(url: String) {
        val intent = Intent(Utils.getApp(), DownloadService::class.java)
        intent.putExtra("task", getTaskByUrl(url))
        intent.putExtra("action", 2)
        ServiceUtils.startService(intent)
    }

    fun resumeDownload(url: String) {
        val intent = Intent(Utils.getApp(), DownloadService::class.java)
        intent.putExtra("task", getTaskByUrl(url))
        intent.putExtra("action", 3)
        ServiceUtils.startService(intent)
    }

    fun deleteDownload(url: String) {
        val intent = Intent(Utils.getApp(), DownloadService::class.java)
        intent.putExtra("task", getTaskByUrl(url))
        intent.putExtra("action", 4)
        ServiceUtils.startService(intent)
    }

    fun getUnSuccessDownload(): ArrayList<DownloadEntity> {
        return downloadManager.getUnSuccessDownload()
    }

    fun getSuccessTasks(complete: (tasks: ArrayList<DownloadEntity>) -> Unit) {
        downloadManager.getSuccessTasks(complete)
    }

    fun getAlbumDownloads(video: Long, complete: (tasks: ArrayList<DownloadEntity>) -> Unit) {
        downloadManager.getAlbumDownloads(video, complete)
    }

    fun setOnlyWifiDownload(isOnlyWifi: Boolean) {
        downloadManager.setOnlyWifiDownload(isOnlyWifi)
    }

    fun isOnlyWifiDownload(): Boolean {
        return downloadManager.isOnlyWifiDownload()
    }

    fun getDownloadTask(video: Long, episode: Int, sourceId: Int, complete: (task: DownloadEntity?) -> Unit) {
        downloadManager.getDownloadTask(video, episode, sourceId, complete)
    }

    fun updateTaskAndSendMessage(task: DownloadEntity) {
        downloadManager.updateTaskAndSendMessage(task)
    }

    fun getTaskByUrl(url: String): DownloadEntity? {
        return downloadManager.getTaskByUrl(url)
    }

}